﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CvModel;
using System.Data.Entity.Migrations;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class AdminDB
    {
        private static CvModelContainer _context = new CvModelContainer();

        //CREATE
        public static int CreateAdmin(Admin admin)
        {
            try
            {
                var query = _context.Admin.Add(admin);
                return _context.SaveChanges();
            }
            catch (Exception)
        	{
		
		        throw;
	        }
            
        }
        //READ
        public static Admin GetAdmin(string userName, string password)
        {
            try
            {
                var query =
                    (from a in _context.Admin
                        where a.UserName == userName && a.Password == password
                        select a).First();
                
                var admin = new Admin()
                {
                    AdminId = query.AdminId,
                    UserName = query.UserName, 
                    AdminTitle = query.AdminTitle, 
                    Email = query.Email, 
                    CompanyName = query.CompanyName,
                    Phone = query.Phone,
                    Address = query.Address, 
                    Zip = query.Zip, 
                    City = query.City, 
                    IsDeleted = query.IsDeleted
                };

                return admin;
            }
            catch (Exception)
	        {
		
		        throw;
	        }
        }

        public static Admin GetAdminOnID(int adminId)
        {
            try
            {
                var query =
                    (from a in _context.Admin
                     where a.AdminId == adminId
                     select a).First();

                var admin = new Admin()
                {
                    AdminId = query.AdminId,
                    UserName = query.UserName,
                    AdminTitle = query.AdminTitle,
                    Email = query.Email,
                    CompanyName = query.CompanyName,
                    Phone = query.Phone,
                    Address = query.Address,
                    Zip = query.Zip,
                    City = query.City,
                    IsDeleted = query.IsDeleted
                };

                return admin;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static List<Admin> GetHeadHunters()
        {
            try
            {
                var query = _context.Admin.Where(a => a.AdminTitle == AdminTitle.Headhunter).ToList();
                return query;

            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public static Admin GetCompany(int workAdId)
        {
            var company =
                (from w in _context.WorkAd
                 from a in _context.Admin
                 where w.WorkAdId == workAdId && w.AdminId == a.AdminId
                 select a).First();
            return company;
        }

        public static bool AdminIsLoggedIn(string userName, string password)
        {
            try
            {
                var query = _context.Admin.Where(a => a.UserName == userName && a.Password == password).ToList();
                if (query.Count > 0)
                {
                    if (query != null)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        //UPDATE
        public static int UpdateAdmin(Admin admin)
        {
            try
            {
                _context.Admin.AddOrUpdate(admin);
                return _context.SaveChanges();
            }
            catch (Exception)
	        {
		
		        throw;
        	}
        }
        //DELETE
        public static int DeleteAdmin(int adminId)
        {
            try
            {
                var query =
                    (from a in _context.Admin
                     where a.AdminId == adminId
                     select a).Single();

                _context.Admin.Remove(query);

                return _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}
